package com.feather.common.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {
	public static void resize(File file, File destFile, int destWidth, int destHeight) throws IOException {
		BufferedImage srcImage = javax.imageio.ImageIO.read(file);
		int srcWidth = srcImage.getWidth(null); // 源图宽
		int srcHeight = srcImage.getHeight(null); // 源图长

		destFile.getParentFile().mkdirs();

		if (destWidth > srcWidth || destWidth < 1) {
			destWidth = srcWidth;
		}
		if (destHeight > srcHeight || destHeight < 1) {
			destHeight = srcHeight;
		}
		resize(srcImage, destFile, destWidth, destHeight);
	}

	/**
	 * 按照固定的比例缩放图片
	 */
	public static void resize(File file, File destFile, double scale) throws IOException {
		BufferedImage srcImage = javax.imageio.ImageIO.read(file);
		int srcWidth = srcImage.getWidth(null); // 源图宽
		int srcHeight = srcImage.getHeight(null); // 源图长

		destFile.getParentFile().mkdirs();

		resize(srcImage, destFile, (int) (srcWidth * scale), (int) (srcHeight * scale));
	}

	/**
	 * 以宽度为基准，等比例放缩图片
	 */
	public void resizeByWidth(File file, File destFile, int width) throws IOException {
		BufferedImage srcImage = javax.imageio.ImageIO.read(file);
		int srcWidth = srcImage.getWidth(null); // 源图宽
		int srcHeight = srcImage.getHeight(null); // 源图长

		destFile.getParentFile().mkdirs();

		if (width > srcWidth || width < 1) {
			width = srcWidth;
		}

		resize(srcImage, destFile, width, (int) (srcHeight * width / srcWidth));
	}

	/**
	 * 以高度为基准，等比例缩放图片
	 */
	public void resizeByHeight(File file, File destFile, int height) throws IOException {
		BufferedImage srcImage = javax.imageio.ImageIO.read(file);
		int srcWidth = srcImage.getWidth(null); // 源图宽
		int srcHeight = srcImage.getHeight(null); // 源图长

		destFile.getParentFile().mkdirs();

		if (height > srcHeight || height < 1) {
			height = srcHeight;
		}
		resize(srcImage, destFile, (int) (srcWidth * height / srcHeight), height);
	}

	/**
	 * 按照最大高度限制，生成最大的等比例缩略图
	 */
	public static void resizeFix(File file, File destFile, int maxWidth, int maxHeight) throws IOException {
		BufferedImage srcImage = javax.imageio.ImageIO.read(file);
		int srcWidth = srcImage.getWidth(null); // 源图宽
		int srcHeight = srcImage.getHeight(null); // 源图长

		destFile.getParentFile().mkdirs();

		if (maxWidth > srcWidth || maxWidth < 1) {
			maxWidth = srcWidth;
		}
		if (maxHeight > srcHeight || maxHeight < 1) {
			maxHeight = srcHeight;
		}

		if (srcWidth / srcHeight > maxWidth / maxHeight) {
			resize(srcImage, destFile, maxWidth, (int) (srcHeight * maxWidth / srcWidth));
		} else {
			resize(srcImage, destFile, (int) (srcWidth * maxHeight / srcHeight), maxHeight);
		}
	}

	public static void resize(BufferedImage srcImage, File destFile, int destWidth, int destHeight) throws IOException {
		FileOutputStream fos = null;
		try {
			BufferedImage destImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
			destImage.getGraphics().drawImage(srcImage, 0, 0, destWidth, destHeight, null);
			fos = new FileOutputStream(destFile);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
			encoder.encode(destImage); // 近JPEG编码
		} catch (IOException ex) {
			throw ex;
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (Exception ex) {
				}
			}
		}
	}

	/**
	 * @return true 彩色图片, false 黑灰图片
	 */
	public static boolean colours(File file) throws IOException {
		BufferedImage src = ImageIO.read(file);
		int height = src.getHeight();
		int width = src.getWidth();
		int[] rgb = new int[4];
		for (int i = 0, count = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				int pixel = src.getRGB(i, j);
				rgb[1] = (pixel & 0xff0000) >> 16;
				rgb[2] = (pixel & 0xff00) >> 8;
				rgb[3] = (pixel & 0xff);
				// 如果像素点不相等的数量超过50个 就判断为彩色图片
				if (rgb[1] != rgb[2] && rgb[2] != rgb[3] && rgb[3] != rgb[1]) {
					count++;
					if (count >= 50)
						return true;
				}
			}
		}
		return false;
	}
}
